package com.lin.fengxiu.lib;

import com.lin.fengxiu.exception.http.ServerErrorException;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

/**
 * @ClassName LinWxNotify
 * @Description TODO 微信支付结果
 * @Author wushaopei
 * @Date 2021/4/11 23:19
 * @Version 1.0
 */
public class LinWxNotify {

    /**
     * @Description TODO 读取回调结果
     * @param stream
     * @return
     */
    public static String readNotify(InputStream stream){
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
        StringBuilder builder = new StringBuilder();

        String line;
        try {
            while ((line = reader.readLine()) != null){
                builder.append(line).append("\n");
            }
        }
        catch (IOException e){
            throw new ServerErrorException(9999);
        }
        finally {
            try {
                stream.close();
            }catch (IOException e){
                e.printStackTrace();
                throw new ServerErrorException(9999);
            }
        }
        return builder.toString();
    }

    /**
     * @Description TODO 回调失败
     * @return
     */
    public static String fail(){
        return "false";
    }

    /**
     * @Description TODO 回调成功
     * @return
     */
    public static String success(){
        return "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>";
    }
}
